<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends  CommonController{
		//登录
    public function login(){

        if(IS_POST){  
/*
              if($_POST['code']){             
                $verify = new \Think\Verify();             
                 $ioc = $verify->check($_POST['code']); //验证码  正确返回1
               }

               if(empty($ioc)){
                   $this->error('验证码错误','',2);die; 
               }*/
                   

            $_POST['password']=md5(I('post.password')); //
           $User = D("admin"); // 实例化User对象,参数是表名
           $data = $User->where(I('post.'))->find(); 

            if(!empty($data)){

                SESSION('id',$data['id']);                
                SESSION('username',$data['username']);
                SESSION('realname',$data['realname']);
                $this->success('登录成功',(U('Index/index')),2);die;
            }else{

                 $this->error('登录失败','',2);die;       
            } 

            
        }
        $this->display();
    }

    /**
     * 验证码
     *
     * @return void
     * @author 
     **/
    function code()
    {
        $Verify = new \Think\Verify();
        ob_clean();//清空之前的缓存
        $Verify->entry();
     }

    /**
     * 管理员添加
     *
     * @return void
     * @author 
     **/
    function add()
    {   
        if(IS_POST){
            // print_r($_POST);die;
           $role= $_POST['role_id'];
            unset($_POST['role_id']);  
            if($_POST['password']==$_POST['passwords']){    //密码和确认密码对比
            $User = D("admin"); //实例化表
            $_POST['password']=md5($_POST['passwords']);  // 加密的密码        
            unset($_POST['passwords']);     //删除加密密码
            $id = $User->data($_POST)->add(); //添加语句
            if($id>0){

                    $data['uid'] = $id;

                    $data['role_id'] = $role;
                   
                    D('user_role')->data($data)->add();
                   $this->success('添加成功',(U('Index/index')),2);die;  
            }else{
                    $this->error('添加失败','',2);die;
            }
          }
      }  
            //查询所有角色
        $role = D('role')->select();
        $this -> assign('role',$role); 
        $this -> display();
    }

        /**
         *注销
         *
         * @return void
         * @author 
         **/
        function session()
        {
           session(null);

           $this->success('',U('Admin/login'),2);die; //注销跳转
        }
    /**
     * 注册
     *
     * @return void
     * @author 
     **/
    function register(){

             $this -> display();

    }

    /**
     * 管理员列表
     *
     * @return void
     * @author 
     **/
    function lists()
    {   
        //查询所有角色
        $data = $this->Model->relation(true)->select();//查询数据库
        
        $this->assign('data',$data);      
        $this->display();
    }

    /**
     *管理员删除
     *
     * @return void
     * @author 
     **/
    function delete()
    {
        if(IN_GET){
        $User = D("admin");//实例化
        $doc = $User->where('id='.$_GET['id'])->delete();
        if($doc>0) {
            $this->success('删除成功','',2);die;
        }else{
            $this->error('删除失败','',2);die; 
        }
        }
    }

    /**
     *管理员编辑
     *
     * @return void
     * @author 
     **/
    function save()
    {
    if (IN_GET) {
    $User = D("admin");
      $id = $_GET;

     $date = $User->where('$id='.$id)->save();
        
    }
    $this->display();
}
    
    
}